﻿using System;
using System.Collections;
using System.Collections.Generic;

using System.Linq;
using System.Web;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;

namespace ACS.Modules.TapaTalk
{
    public class TapatalkSettings
    {
        #region " Private Members "

        private Hashtable _allSetings;
        private int _tabModuleId = -1;
        private int _forumModuleID = -1;
        private int _forumTabID = -1;
        private bool _Is_Open = false;
        private bool _Guest_Access = false;
        private bool _Disable_Search = false;
        private bool _Disable_Latest = false;
        private bool _Disable_Pm = false;

        #endregion

        #region " Properties "

        public int forumTabID
        {
            get { return _forumTabID; }
            set { _forumTabID = value; }
        }
        public int forumModuleID
        {
            get { return _forumModuleID; }
            set { _forumModuleID = value; }
        }
        public bool Is_Open
        {
            get { return _Is_Open; }
            set { _Is_Open = value; }
        }
        public bool Guest_Access
        {
            get { return _Guest_Access; }
            set { _Guest_Access = value; }
        }
        public bool Disable_Search
        {
            get { return _Disable_Search; }
            set { _Disable_Search = value; }
        }
        public bool Disable_Latest
        {
            get { return _Disable_Latest; }
            set { _Disable_Latest = value; }
        }
        public bool Disable_Pm
        {
            get { return _Disable_Pm; }
            set { _Disable_Pm = value; }
        }
        public  Hashtable AllSettings
        {
            get { return _allSetings; }
        }
        #endregion

        #region " Constructor "
        public TapatalkSettings(int TabModuleId)
        {
            _tabModuleId = TabModuleId;
            _allSetings =new DotNetNuke.Entities.Modules.ModuleController().GetTabModuleSettings(_tabModuleId);
            Common.ReadValue(ref _allSetings, "forumTabID",ref _forumTabID);
            Common.ReadValue(ref _allSetings, "forumModuleID", ref _forumModuleID);
            Common.ReadValue(ref _allSetings, "Is_Open", ref _Is_Open);
            Common.ReadValue(ref _allSetings, "Guest_Access", ref _Guest_Access);
            Common.ReadValue(ref _allSetings, "Disable_Search", ref _Disable_Search);
            Common.ReadValue(ref _allSetings, "Disable_Latest", ref _Disable_Latest);
            Common.ReadValue(ref _allSetings, "Disable_Pm", ref _Disable_Pm);
        }

        public static TapatalkSettings GetTapatalkSettings(int TabModuleId) {
            string CacheKey = "TapatalkSettings_" + TabModuleId.ToString();
            TapatalkSettings ts = (TapatalkSettings)DataCache.GetCache(CacheKey);
            if (ts == null)
            {
                ts = new TapatalkSettings(TabModuleId);
                DataCache.SetCache(CacheKey, ts);
            }
            return ts;
        }


        #endregion

        #region " Public Members"
        public  void UpdateSettings()
        {

            DotNetNuke.Entities.Modules.ModuleController objModules = new DotNetNuke.Entities.Modules.ModuleController();
           
                objModules.UpdateTabModuleSetting(_tabModuleId, "forumTabID", forumTabID.ToString());
                objModules.UpdateTabModuleSetting(_tabModuleId, "forumModuleID", forumModuleID.ToString());
                objModules.UpdateTabModuleSetting(_tabModuleId, "Is_Open", Is_Open.ToString());
                objModules.UpdateTabModuleSetting(_tabModuleId, "Guest_Access", Guest_Access.ToString());
                objModules.UpdateTabModuleSetting(_tabModuleId, "Disable_Search", Disable_Search.ToString());
                objModules.UpdateTabModuleSetting(_tabModuleId, "Disable_Latest", Disable_Latest.ToString());
                objModules.UpdateTabModuleSetting(_tabModuleId, "Disable_Pm", Disable_Pm.ToString());

            string CacheKey = "TapatalkSettings_" + _tabModuleId.ToString();
            DotNetNuke.Common.Utilities.DataCache.RemoveCache(CacheKey);

        }
        #endregion
    }

    public class TapatalkModuleBase : ModuleSettingsBase
    {
        private TapatalkSettings _settings;

        public new TapatalkSettings Settings
        {
            get
            {
                if (_settings == null)
                {
                    _settings = new TapatalkSettings(TabModuleId);
                }
                return _settings;
            }
            set { _settings = value; }
        }


    }

    #region "Common Methods"
    public static class Common
    {
        #region " Utilities"
      //convertir a base64
        public static byte[] StringToBytes(String cadena)
        {
            System.Text.ASCIIEncoding codificador = new System.Text.ASCIIEncoding();
            return codificador.GetBytes(cadena);
        }

        // IsNumeric Function
        public static bool IsNumeric(object Expression)
        {
            // Variable to collect the Return value of the TryParse method.
            bool isNum;

            // Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
            double retNum;

            // The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
            // The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
            isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
            return isNum;
        }
  #endregion

        #region " Value Reading "
        public static void ReadValue(ref Hashtable ValueTable, string ValueName, ref int Variable)
        {
            if ((ValueTable[ValueName] != null))
            {
                try
                {
                    Variable = Convert.ToInt32(ValueTable[ValueName]);
                }
                catch (Exception ex)
                {
                }
            }
        }

        public static void ReadValue(ref Hashtable ValueTable, string ValueName, ref string Variable)
        {
            if ((ValueTable[ValueName] != null))
            {
                try
                {
                    Variable = Convert.ToString(ValueTable[ValueName]);
                }
                catch (Exception ex)
                {
                }
            }
        }

        public static void ReadValue(ref Hashtable ValueTable, string ValueName, ref bool Variable)
        {
            if ((ValueTable[ValueName] != null))
            {
                try
                {
                    Variable = Convert.ToBoolean(ValueTable[ValueName]);
                }
                catch (Exception ex)
                {
                }
            }
        }


        #endregion

    }
    #endregion

}